#include <iostream>

using namespace std;

const int N = 2010;
int a[N];
int f[N][N];
int n;

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= n; i++) f[i][i] = a[i] * n;
	for(int len = 1; len <= n; len++)
	{
		for(int i = 1; i + len - 1 <= n; i++)
		{
			int j = i + len - 1;
			int d = n - len + 1;
			f[i][j] = max(f[i + 1][j] + d * a[i], f[i][j - 1] + d * a[j]);
		}
	}
	cout << f[1][n] << endl;
	return 0;
}
